package com.apts.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apts.jfinalbase.BaseController;
import com.apts.jfinalbase.BaseJsonPojo;
import com.apts.pojo.NomalPojo;
import com.apts.servise.ProductServise;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

// API : /Product
//产品的信息的添加，删除，查询
public class ProductController extends BaseController {

	// 增加产品类型*****增加产品类型*******增加产品类型*********增加产品类型
	public void AddProduct() {
		System.out.println("添加品种信息");

		Map<String, Object> map = new HashMap<String, Object>(0);
		UploadFile file = getFile("file");
		map.put("typecode", this.getPara("typecode"));
		map.put("typename", this.getPara("typename"));
		map.put("profileimg", file.getUploadPath());
		map.put("typeinfo", this.getPara("typeinfo"));

		System.out.println("file名" + file.getFileName());
		System.out.println("file路径" + file.getUploadPath());
		ProductServise.Service.Add(map);
		this.renderJson(new NomalPojo());
	}

	// *********跳到增加产品类型界面
	public void test() {
		render("/upload.html");
	}

	// *****************查找所有产品类型
	public void findall() {
//		int start = this.getParaToInt("start");
//		int end = this.getParaToInt("end");
		//start = start * end;
		System.out.println("全部类型信息查找");
		BaseJsonPojo json = ProductServise.Service.Findall();
		this.renderJson(json);
	}

	// *****************根据产品类型编号查找产品
	public void findbyid() {
		String typecode = this.getPara("typecode");
		System.out.println("类型信息查找");
		BaseJsonPojo json = ProductServise.Service.Findbyid(typecode);
		this.renderJson(json);
	}

	// *****************根据产品名称查找产品
	public void findbytypename() {
		String typename=this.getPara("typename");
		BaseJsonPojo json = ProductServise.Service.Findbytypename(typename);
		this.renderJson(json);
	}
	
	//根据产品描述查找产品
	public void findbytypeinfo(){
		String typeinfo =this.getPara("typeinfo");
		
		List<Record> list = Db.find("select * from tb_product_type where typeinfo like '%" + typeinfo + "%' order by id desc");
		List<Map<String, Object>> productSet = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> map = new HashMap<String, Object>(0);
			Record producttype = list.get(i);
			map.put("proID", producttype.get("typecode"));
			map.put("proName", producttype.get("typename"));
			map.put("proImg", producttype.get("profileimg"));
			map.put("proInfo", producttype.get("typeinfo"));
			productSet.add(map);
		}
		Map<String, Object> result = new HashMap<String, Object>(0);
		result.put("count", productSet.size());
		result.put("list", productSet);
		
		this.renderJson(new NomalPojo(result));
	}

	// *****************根据企业名查找品种
	public void findbyenterprise() {
		String epname=this.getPara("epname");
		int start = this.getParaToInt("start");
		int end = this.getParaToInt("end");
		//start = start * end;
		BaseJsonPojo json = ProductServise.Service.Findbyenterprise(epname, start, end);
		this.renderJson(json);
	}

	// 品种添加企业
	public void addenterprise() {
		String typecode = this.getPara("typecode");
		String regcode = this.getPara("regcode");
		ProductServise.Service.Addenterprise(typecode, regcode);
		this.renderJson(new NomalPojo());

	}
	
	// 添加品种
	public void addProduct(){
		String typecode = String.valueOf( new Date().getTime() );
		String typename = this.getPara("typename");
		String profileimg = this.getPara("profileimg");
		String typeinfo = this.getPara("typeinfo");
		Db.update("insert into tb_product_type (typecode,typename,profileimg,typeinfo) values ( ?, ?, ?, ? ) ", typecode, typename,profileimg,typeinfo );
		this.renderJson(new NomalPojo());
	}

	// 品种删除企业
	public void DeleteEnterprise() {
		String typecode = this.getPara("typecode");
		String regcode = this.getPara("regcode");
		ProductServise.Service.deleteenterprise(typecode, regcode);
		this.renderJson(new NomalPojo());

	}

	// *********************删除产品类型
	public void delete() {
		String typecode = this.getPara("typecode");
		ProductServise.Service.Delete(typecode);
		System.out.println("删除类型信息");
		this.renderJson(new NomalPojo());
	}

	public void updateProduct() {
		System.out.println("修改品种信息");
		Map<String, Object> map = new HashMap<String, Object>(0);
//		UploadFile file = getFile("file");
		map.put("id", this.getPara("id"));
		map.put("typecode", this.getPara("typecode"));
		map.put("typename", this.getPara("typename"));
		map.put("profileimg", this.getPara("typeimg"));
		map.put("typeinfo", this.getPara("typeinfo"));
//		System.out.println("file名" + file.getFileName());
//		System.out.println("file路径" + file.getUploadPath());
		ProductServise.Service.Update(map);
		this.renderJson(new NomalPojo());

	}

	public void test1() {
		render("/editproduct.html");
	}

}
